package ru.ntcreb.csir.libs.methods.SVCH.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.xml.sax.SAXException;

import ru.ntcreb.csir.libs.methods.SVCH.xml.handler.ComprehensiveTractParserHandler;
import ru.ntcreb.csir.libs.methods.SVCH.xml.handler.SVCHImpulseParserHandler;
import ru.ntcreb.csir.libs.methods.SVCH.xml.handler.SVCHImpulseReactionParserHandler;
import ru.ntcreb.csir.libs.methods.SVCH.xml.handler.TractParserHandler;

public class XMLParserService {

	private static final Logger log = LogManager
			.getLogger(XMLParserService.class);

	private SAXParserFactory factory = SAXParserFactory.newInstance();

	public Map<Double, Double> parseTractArray(String xml) {

		Map<Double, Double> result = new HashMap<>();
		try {
			SAXParser saxParser = factory.newSAXParser();

			saxParser.parse(
					new ByteArrayInputStream(xml
							.getBytes(StandardCharsets.UTF_8)),
					new TractParserHandler(result));

		} catch (SAXException | ParserConfigurationException | IOException e) {
			log.error("Problem occur while parsing xml:");
			log.error(xml);
			log.error(e.getMessage());

			return new HashMap<>();
		}

		return result;

	}

	public Map<Double, Double> parseComprehensiveTractArray(String xml) {

		Map<Double, Double> result = new HashMap<>();
		try {
			SAXParser saxParser = factory.newSAXParser();

			saxParser.parse(
					new ByteArrayInputStream(xml
							.getBytes(StandardCharsets.UTF_8)),
					new ComprehensiveTractParserHandler(result));

		} catch (SAXException | ParserConfigurationException | IOException e) {
			log.error("Problem occur while parsing xml:");
			log.error(xml);
			log.error(e.getMessage());

			return new HashMap<>();
		}

		return result;

	}

	public Map<Double, Double> parseSVCHImpulseArray(String xml) {

		Map<Double, Double> result = new HashMap<>();
		try {
			SAXParser saxParser = factory.newSAXParser();

			saxParser.parse(
					new ByteArrayInputStream(xml
							.getBytes(StandardCharsets.UTF_8)),
					new SVCHImpulseParserHandler(result));

		} catch (SAXException | ParserConfigurationException | IOException e) {
			log.error("Problem occur while parsing xml:");
			log.error(xml);
			log.error(e.getMessage());

			return new HashMap<>();
		}

		return result;

	}

	public Map<Double, Double> parseSVCHImpulseReactionArray(String xml) {

		Map<Double, Double> result = new HashMap<>();
		try {
			SAXParser saxParser = factory.newSAXParser();

			saxParser.parse(
					new ByteArrayInputStream(xml
							.getBytes(StandardCharsets.UTF_8)),
					new SVCHImpulseReactionParserHandler(result));

		} catch (SAXException | ParserConfigurationException | IOException e) {
			log.error("Problem occur while parsing xml:");
			log.error(xml);
			log.error(e.getMessage());

			return new HashMap<>();
		}

		return result;

	}

}
